//
//  QianbaoViewController.swift
//  联付商圈
//
//  Created by zhangqi on 2017/7/20.
//  Copyright © 2017年 lianfu. All rights reserved.
//

//@implementation SQUserModel
//
//+ (NSDictionary *)modelCustomPropertyMapper {
//    return @{@"userName"   : @"name",
//        @"userImage"  : @"user_img",
//        @"shopName"   : @"shopname",
//        @"isRealName" : @"smrz"};
//}
//
//@interface WithdrawViewController : UIViewController
//
//- (void)refreshData;



import UIKit
import Alamofire

@objc class QianbaoViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {

    @IBAction func unwindToQianbaoScreen(segue:UIStoryboardSegue) {
    }
    var imageArray = ["结算记录","收支明细","银行卡管理","提现设置"]
    var labelArray = ["提现记录","收支明细","银行卡管理","提现"]
    var CYView = CYPasswordView()
    @IBOutlet weak var titnumLab: UILabel!
    
    @IBOutlet weak var TableView: UITableView!
    
    
    @IBAction func dis(_ sender: UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func tixianBtn(_ sender: UIButton) {
    }
    
    func tanchuang(){
        
        let alertVC = UIAlertController(title: "您未绑定银行卡", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        let acSure = UIAlertAction(title: "去绑定", style: UIAlertActionStyle.destructive) { (UIAlertAction) -> Void in
            self.tiaozhuan()
        }
        let acCancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (UIAlertAction) -> Void in
            return
        }
        alertVC.addAction(acSure)
        alertVC.addAction(acCancel)
        self.present(alertVC, animated: true, completion: nil)
    }
    
    func tiaozhuan(){
        let bankCardVC = BoundBankCardViewController.init()
        self.navigationController?.pushViewController(bankCardVC, animated: true)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    self.navigationController?.navigationBar.isHidden = false
        let x = UserDefaults.standard.object(forKey: "userInfo") as! NSDictionary
        let y = x["money"]!
        //SQUserModel *model = [SQUserModel yy_modelWithDictionary:dic];
//        let z = SQUserModel()
//        print(z.user_id)
        //print(z.)
        titnumLab.text = "\(y)"

        
        
        // 通知
        //        NSString *const CYPasswordViewCancleButtonClickNotification = @"CYPasswordViewCancleButtonClickNotification";
        //        NSString *const CYPasswordViewDeleteButtonClickNotification = @"CYPasswordViewDeleteButtonClickNotification";
        //        NSString *const CYPasswordViewNumberButtonClickNotification = @"CYPasswordViewNumberButtonClickNotification";
        //        NSString *const CYPasswordViewForgetPWDButtonClickNotification = @"CYPasswordViewForgetPWDButtonClickNotification";
        //        NSString *const CYPasswordViewEnabledUserInteractionNotification = @"CYPasswordViewEnabledUserInteractionNotification";
        //        NSString *const CYPasswordViewDisEnabledUserInteractionNotification = @"CYPasswordViewDisEnabledUserInteractionNotification";
        NotificationCenter.default.addObserver(self, selector: #selector(forget), name: NSNotification.Name.CYPasswordViewForgetPWDButtonClick, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(cancel), name: NSNotification.Name.CYPasswordViewCancleButtonClick, object: nil)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    
    func forget(){
        self.noticeInfo("退出账号重新登录", autoClear: true, autoClearTime: 1)
    }
    func cancel(){
        //print("取消输入密码")
    }
    
    func yanzhengmima(mima:String){
        let x = UserDefaults.standard.object(forKey: "userInfo") as! NSDictionary
        let y = x["name"]!
        let par = ["name":"\(y)","zfmm":mima]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/zfmm?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: par).responseJSON {Request in
            switch Request.result {
            case .success:
                let json = JSON(data: Request.data!).dictionaryObject!
                //print(json)
                for t in json.keys{
                    let a = t
                    if a == "error" {
                        self.noticeInfo("密码验证错误", autoClear: true, autoClearTime: 1)
                        self.CYView.hide()
                        return
                    } else if a == "success" {
                        self.CYView.removeFromSuperview()
                        guanliORtixian = "guanli"
                        let sb = UIStoryboard(name: "Main", bundle:nil)
                        let vc = sb.instantiateViewController(withIdentifier: "GuanliViewController") as! GuanliViewController

                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
                self.CYView.hide()
            }
        }
    }

    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return labelArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "QianbaoOneTableViewCell", for: indexPath) as! QianbaoOneTableViewCell
        
            cell.oneImage.image = UIImage(named:imageArray[indexPath.row])
            cell.oneImage.layer.cornerRadius = 20
            cell.oneLabel.text = labelArray[indexPath.row]
            
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        TableView.deselectRow(at: indexPath, animated: false)
            switch indexPath.row {
            case 0:
                let sb = UIStoryboard(name: "Main", bundle:nil)
                let vc = sb.instantiateViewController(withIdentifier: "TixianJiluViewController") as! TixianJiluViewController
                self.navigationController?.pushViewController(vc, animated: true)
            case 1:
                let sb = UIStoryboard(name: "Main", bundle:nil)
                let vc = sb.instantiateViewController(withIdentifier: "MingxiViewController") as! MingxiViewController
                self.navigationController?.pushViewController(vc, animated: true)
            case 2:
                self.CYView = CYPasswordView()
                let keyWindow = UIApplication.shared.keyWindow!
                CYView.show(in: keyWindow)
                CYView.finish = {
                    password -> Void in
                    let x = password
                    //print(x!)
                    self.yanzhengmima(mima:"\(x!)")
                }
            case 3:

                guanliORtixian = "tixian"
                self.navigationController?.pushViewController(WithdrawViewController(), animated: true)
//            case 4:
//                let creditCardVC = CreditCardViewController.init()
//                self.navigationController?.pushViewController(creditCardVC, animated: true)
//            case 5:
//                let DaiKuanVc = ApplyDaiKuanViewController.init()
//                self.navigationController?.pushViewController(DaiKuanVc, animated: true)
            default:
                break
            }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headTitle: UILabel = UILabel()
            headTitle.backgroundColor = UIColor.clear
            return headTitle
        }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 12
    }
    
   
}
